package com.qidian.server.workerThread;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.qidian.common.Globals;
import com.qidian.common.beans.UserBean;
import com.qidian.connection.util.ConnectionPool;

public final class Synchronizer_User extends WorkerThread
{
	private static Logger logger = Logger.getLogger(Synchronizer_User.class) ;
	private static Synchronizer_User userSynchronizer = new Synchronizer_User() ;
	
//    int interval = 30*1000 ;
    int batch_size = 100 ;
    
    private Synchronizer_User()
    {
    	
    }
    
    public void run()
    {
        UserBean user = new UserBean() ;
        int loopTimes = 0 ;
        try
        {
            ConcurrentHashMap<String,UserBean> userBuf = Globals.getUserBuffer().getBuffer();
            while(keepRunning)
            {
            	loopTimes ++ ;
                Connection conn = null ;
                PreparedStatement ps = null ;
                try
                {
                    conn = ConnectionPool.getConnection() ;
                    ps = user.getPreparedStatement(conn) ;
                    int i = 0 ;
                    Iterator<UserBean> userItr = userBuf.values().iterator() ;
                    while(userItr.hasNext())
                    {
                        user = userItr.next() ;
//                        if (user.needSynchronized() || (loopTimes % batch_size == (batch_size-1)))
                        if (user.needSynchronized())
                        {
                            if(!user.setParameters(ps))
                                continue;
                            ps.addBatch() ;
                            i++ ;
                            if (i%batch_size == (batch_size-1))
                            {
                                ps.executeBatch() ;
                                ps.clearBatch() ;
                            }
                        }
                        
                        if (user.isDeleted())
                        	Globals.getUserBuffer().remove(user.getUserId()) ;
                    }
                    
                    ps.executeBatch() ;
                    ps.clearBatch() ;
                    ps.close() ;
                    
                }
                catch (SQLException e)
                {
                    e.printStackTrace() ;
                    logger.error(e.getMessage()) ;
                }
                finally{
                	ConnectionPool.freeConn(conn) ;
                }
                
                Thread.sleep(interval) ;
            }
        }
        catch (Exception e)
        {
            e.getMessage() ;
            logger.error(e.getMessage()) ;
            System.exit(-1) ;
        }
    }

    @Override
    public void init()
    {
        // TODO Auto-generated method stub
        
    }

	public static Synchronizer_User getInstance() 
	{
		return userSynchronizer;
	}
}
